﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using HeadLinx.AD.Common;
using System.Diagnostics;
using System.IO;

namespace HeadLinx.AD.Explorer
{
    /// <summary>
    /// Interaction logic for PersonCardView.xaml
    /// </summary>
    public partial class PersonCardView : UserControl
    {
        private HeadLinxUser _user;
        private Brush _originalCardBackground;

        public PersonCardView(HeadLinxUser user)
        {
            _user = user;

            InitializeComponent();

            if (user.SelectedUser)
            {
                card.Background = Brushes.DarkGoldenrod;
            }

            _originalCardBackground = card.Background; // for restoring temporary highlight

            if (String.IsNullOrEmpty(user.HomePage))
            {
                menuBrowseHomePage.IsEnabled = false;
            }

            userDisplayName.Text = GetUserBannerName(user);

            thumbnailPhoto.Source = GetUserThumbnailPhoto(user);

            if (thumbnailPhoto.Source == null)
            {
                // No thumbnail photo, collapse the photo column.
                photoColumn.Width = new GridLength(0);
            }

            menuCopyClipboard.Header = String.Format("Copy {0} to Clipboard", user.FullName);

            userDetails.Text = ConstructUserDetails(user);
        }

        public HeadLinxUser User
        {
            get
            {
                return _user;
            }
        }

        private string GetUserBannerName(HeadLinxUser user)
        {
            return String.Format("{0} {1} ({2})",
                user.FirstName, user.LastName, user.EmailAlias);
        }

        private BitmapImage GetUserThumbnailPhoto(HeadLinxUser user)
        {
            try
            {
                if (!String.IsNullOrEmpty(user.ThumbnailPhotoPath))
                {
                    return BitmapUtility.FromFile(user.ThumbnailPhotoPath);
                }
                else if (user.ThumbnailPhotoData != null)
                {
                    return BitmapUtility.FromByteArray(user.ThumbnailPhotoData);
                }
            }
            catch (Exception ex)
            {
                Trace.TraceError("Error loading photo for {0}: {1}", user.EmailAlias, ex.Message);
            }

            return null;
        }

        private string ConstructUserDetails(HeadLinxUser user)
        {
            StringBuilder sb = new StringBuilder();

            // Basic info
            sb.AppendLine("Title: " + user.Title);
            sb.AppendLine("Company: " + user.Company);
            sb.AppendLine("Department: " + user.Department);

            // Contact info
            sb.AppendLine();
            sb.AppendLine("Office: " + user.PhysicalOffice);
            sb.AppendLine("Phone: " + user.Phone);
            sb.AppendLine("Mobile: " + user.MobilePhone);

            // Home page
            if (!String.IsNullOrEmpty(user.HomePage))
            {
                sb.AppendLine();
                sb.Append("Home page: " + user.HomePage);
            }

            return sb.ToString();
        }

        private void menuClipAll_Click(object sender, RoutedEventArgs e)
        {
            LocalUtility.ClipAllInformation(_user, false);
        }

        private void menuClipContact_Click(object sender, RoutedEventArgs e)
        {
            LocalUtility.ClipContactInformation(_user, false);
        }

        private void menuShowDirectReports_Click(object sender, RoutedEventArgs e)
        {
            string title = "Direct reports of " + _user.EmailAlias;
            LocalUtility.RunQueryAndShowResults(this, HeadLinxQueryType.DirectReports, _user.EmailAlias, title, false);
        }

        private void menuShowPeers_Click(object sender, RoutedEventArgs e)
        {
            string title = "Peers of " + _user.EmailAlias;
            LocalUtility.RunQueryAndShowResults(this, HeadLinxQueryType.Peers, _user.EmailAlias, title, false);
        }

        private void menuShowSuperiors_Click(object sender, RoutedEventArgs e)
        {
            string title = "Superiors of " + _user.EmailAlias;
            LocalUtility.RunQueryAndShowResults(this, HeadLinxQueryType.Superiors, _user.EmailAlias, title, false);
        }

        private void menuShowSubtree_Click(object sender, RoutedEventArgs e)
        {
            ChartView dlg = new ChartView(_user, null);
            dlg.ShowDialog();
        }

        private void menuBrowseHomePage_Click(object sender, RoutedEventArgs e)
        {
            if (!String.IsNullOrEmpty(_user.HomePage))
            {
                try
                {
                    Uri uri = new Uri(_user.HomePage, UriKind.Absolute);
                    Process.Start(uri.ToString());
                }
                catch (Exception ex)
                {
                    // Ignore and log invalid URI.
                    Trace.TraceError("Error parsing homepage URL: " + ex.Message);
                }
            }
        }

        private void contextMenu_Opened(object sender, RoutedEventArgs e)
        {
            card.Background = Brushes.YellowGreen;
        }

        private void contextMenu_Closed(object sender, RoutedEventArgs e)
        {
            card.Background = _originalCardBackground;
        }
    }
}
